<?php
/**
 * Created by lihuan.
 * User: lihuan
 * Date: 2018/3/29 0029
 * Time: 下午 2:43
 */
namespace app\wxmsg\controller;

class EventMsg {

    //处理普通事件消息
    public function handelMsg($postObj){
    
        $content = "";
        switch ($postObj->Event) {
            case "subscribe":
                //关注事件
                $content = $this->subscribeContent();
                break;
            case "unsubscribe":
                //取消关注事件
                break;
            default:
                $content = "Unknow Event: " . $postObj->Event;
                break;
        }
    
        //构造模板返回给微信
        $resultStr = sprintf($this->getTextTemplate(), $postObj->FromUserName, $postObj->ToUserName, time(), $content, 0);
        return $resultStr;
    }
    
    private function getTextTemplate(){
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        return $textTpl;
    }
    
    private function subscribeContent(){
        $content = "欢迎来到此公众号!";
        return $content;
    }
}